package com.softtown.modules.organization.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.enums.YesOrNoEnum;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.organization.dao.CompOrgEmplPosnDao;
import com.softtown.modules.organization.dto.CompOrgPosnEmplPageDTO;
import com.softtown.modules.organization.entity.CompOrgEmplPosnEntity;
import com.softtown.modules.organization.service.CompOrgEmplPosnService;
import com.softtown.modules.organization.vo.CompOrgPosnEmplVO;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.security.user.UserDetail;
import com.softtown.modules.sys.entity.CompSysParamEntity;
import com.softtown.modules.sys.service.CompSysParamService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: Dingyayong
 * @Date: 2019/6/22 09:39
 * @Description:
 */
@Service
public class CompOrgEmplPosnServiceImpl extends BaseServiceImpl<CompOrgEmplPosnDao, CompOrgEmplPosnEntity> implements CompOrgEmplPosnService {

    @Resource
    private CompSysParamService compSysParamService;

    @Override
    public List<CompOrgEmplPosnEntity> findListPosnEntity(Long compId, Long emplId) {
        return baseDao.findListPosnEntity(compId,emplId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEmplPosn(CompOrgEmplPosnEntity compOrgEmplPosnEntity) {
        baseDao.updateEmplPosn(compOrgEmplPosnEntity);
    }

    /**
     * @Title: getPosnIdByEmplIdAndCompId
     * @Description: 根据员工ID和公司ID来查找员工岗位里的岗位IDService实现方法
     * @param @param [emplId, compId]
     * @return @return java.util.List<java.lang.Long>
     * @author fuming
     * @throws
     * @date 2019/7/4 16:24
     */
    @Override
    public List<Long> getPosnIdByEmplIdAndCompId(Long emplId, Long compId) {
        return baseDao.getPosnIdByEmplIdAndCompId(emplId, compId);
    }

    @Override
    public List<String> getPosnNameByEmplIdAndCompId(Long emplId, Long compId) {
        return baseDao.getPosnNameByEmplIdAndCompId(emplId,compId);
    }

    @Override
    public PageInfo getPageOrgPosnEmpl(CompOrgPosnEmplPageDTO compOrgPosnEmplPageDTO) {
        CompSysParamEntity compSysParamEntity = compSysParamService.findValueByCompAndCode(compOrgPosnEmplPageDTO.getCompId(), "SELF_APRV_IS_OK");
        String isSelf = null;
        if (compSysParamEntity.getParamValue().equals(YesOrNoEnum.NO.value())) {
            isSelf = YesOrNoEnum.NO.value();
        }
        Page page = PageHelper.startPage(compOrgPosnEmplPageDTO.getPage(), compOrgPosnEmplPageDTO.getPageSize());
        UserDetail userDetail = SecurityUser.getUser();
        List<CompOrgEmplPosnEntity> compOrgEmplPosnEntities = baseDao.selectAppoverPosnAndEmpl(compOrgPosnEmplPageDTO.getParam(),
                userDetail.getId(), compOrgPosnEmplPageDTO.getCompId(), isSelf);
        List<CompOrgPosnEmplVO> list = ConvertUtils.sourceToTarget(compOrgEmplPosnEntities, CompOrgPosnEmplVO.class);
        return ConvertUtils.pageInfoToTarget(page, list);
    }
}
